﻿#region License
//
// Author: Pål Fossmo <pal@fossmo.net>
// Copyright (c) 2008 Pål Fossmo
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion
using System.IO;
using System.Linq;
using System.Xml.Linq;
using ICSharpCode.SharpZipLib.Zip;
using TreeFrog.Document;

namespace TreeFrog.Package
{
    public class PackageService : IPackage
    {
        private readonly XDocument _document;

        public PackageService(IDocument document)
        {
            _document = document.GetXml();
        }

        public string InitFolder { get; set; }

        #region IPackage Members

        public void unPack()
        {
            var allDirectoriesAndFiles = from element in _document.Elements("Actions").Elements("Action")
                                         select new
                                                    {
                                                        directory =
                                             TreeFrogPackageExtentionMethod.Value_(
                                             (XElement) element.Element("Directory")),
                                                        files =
                                             from xElement in
                                                 Extensions.Elements<XElement>(element.Elements("Files"), "File")
                                             where
                                                 TreeFrogPackageExtentionMethod.Value_(
                                                     (XAttribute) xElement.Attribute("type")) == "zip"
                                             select xElement
                                                    };

            foreach (var directoryAndFiles in allDirectoriesAndFiles)
            {
                foreach (XElement files in directoryAndFiles.files)
                {
                    string filename = Path.GetFileName(files.Value);
                    string zipfilename = Path.Combine(Path.Combine(InitFolder, directoryAndFiles.directory), filename);
                    string directory = directoryAndFiles.directory;

                    UnzipFiles(zipfilename, directory);
                    CleanUp(filename);
                }
            }
        }

        #endregion

        private void UnzipFiles(string zipfilename, string directory)
        {
            var fastZip = new FastZip();
            fastZip.ExtractZip(zipfilename, Path.Combine(InitFolder, directory), "");
        }

        private void CleanUp(string filename)
        {
            File.Delete(filename);
        }
    }

    public static class TreeFrogPackageExtentionMethod
    {
        public static string Value_(this XElement element)
        {
            if (element == null) return "";
            else
            {
                return element.Value;
            }
        }

        public static string Value_(this XAttribute attribute)
        {
            if (attribute == null) return "";
            else
            {
                return attribute.Value;
            }
        }
    }
}